<?php

namespace app\common\entity;

use think\Db;
use think\Model;
use app\common\entity\Coindeals;
use app\common\entity\MywalletLog;
use app\common\entity\WithdrawRatio;
use think\facade\Log;
use app\common\entity\Config;

class Market extends Model
{
    protected $table = 'otc_market';

    protected $create_time = 'create_time';
    protected $matching_time = 'matching_time';
    protected $autoWriteTimestamp = true;
    #添加申请
    public function addMarketList($uid, $data)
    {

        $entity = new self();
        if ($this->checkOrder($uid)) {
            throw new \Exception('你还有交易未完成，请先去完成交易');
        }
        $entity->title = $data['title'];
        $entity->user_id = $uid;
        $entity->num = $data['total'];
        $entity->price = $data['price'];
        $entity->min = $data['min'];
        $entity->pay_type = $data['pay_type'];
        $entity->charges = Config::getValue('charges');
        $entity->total = $data['total'];
        $entity->create_time = time();
        $entity->status = 1;
        $entity->trade_type = $data['trade_type'];
        $result = $entity->save();
        if ($result) {
            return $entity;
        }
    }
    /**
     * 判断用户是否还有交易没完成
     */
    public function checkOrder($userId) {


        $total = self::where('user_id', $userId)
            ->where('status', 1)
            ->count();
        return $total >= 1 ? true : false;
    }


}
